package torpedo_client.guicomponents;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import torpedo_client.BigShip;
import torpedo_client.Position;
import torpedo_client.Gui;
import torpedo_client.MedShip;
import torpedo_client.MotherShip;
import torpedo_client.SmallShip;

public class BattleSquare extends JPanel {

    private int index;
    public static int bNum = 0, mNum = 0, meNum = 0, sNum = 0;
    static int counter = 0, x = 0, y = 0;
    public static Color Def_BackgroundColor = Color.LIGHT_GRAY;
    public static Color OnFocus_BackgroundColor = Color.DARK_GRAY;
    public static Color Ship_BackgroundColor = Color.GREEN;
    private boolean ACTIVE = true, VERTICAL = true, SHIP = false, HITTED = false;
    public static LinkedList<BattleSquare> mainlist = new LinkedList<BattleSquare>();
    public static ArrayList<Position> positionlist = new ArrayList();
    private Position position;
    MotherShip newMoShip;
    BigShip newBiShip;
    SmallShip newSmShip;
    MedShip newMeShip;

    public static LinkedList<BattleSquare> getTheListOfTheSquares() {
        return mainlist;
    }
    
    public BattleSquare() {}

    public BattleSquare(boolean active) {

        this.ACTIVE = active;
        this.position = new Position(x, y);

        this.index = counter;
        counter++;
        x++;
        if (x == 8) {
            x = 0;
            y++;
        }
        this.addMouseListener(listener);
        this.setBackground(Def_BackgroundColor);
        this.setBorder(BorderFactory.createLineBorder(Color.black));
        updateList();
    }

    private void updateList() {
        mainlist.add(this);
    }

    public int getIndex() {
        return index;
    }

    //van e rajta hajó
    public void setOnShip(boolean SHIP) {
        this.SHIP = SHIP;
    }

    //levan e rakva minden hajó
    public static boolean testShips() {
        if (bNum == 1 && mNum == 1 && meNum == 1 && sNum == 1) {
            return true;
        } else {
            return false;
        }
    }

    public boolean shipReturn() {
        if (newMoShip.getLife() == 0 && newMeShip.getLife() == 0 && newBiShip.getLife() == 0
                && newSmShip.getLife() == 0) {
            return false;
        }
        return true;
    }

    public boolean getShip() {
        return SHIP;
    }

    public void setIndex(int Index) {
        this.index = Index;
    }

    public void setHit() {
        HITTED = true;
    }
    
    public Position getHit() {
        return getPos();
    }
    
    public boolean hitted() {
        if(this.HITTED)
        {
            return true;
        } else {
            return false;
        }
    }
    
    public Position getPos() {
        return position;
    }
    MouseListener listener = new MouseListener() {

        @Override
        public void mousePressed(MouseEvent e) {

            //enemy field lövés
            if (BattleSquare.this.ACTIVE) {
                BattleSquare.this.setBackground(Color.RED);
                System.out.println(BattleSquare.this.getHit());
                BattleSquare.this.HITTED = true;
            }
            
            //our fieldre rakás       
            //vízszintes lerakás
            if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
                VERTICAL = !VERTICAL;
                repaint();
            }
            if (!BattleSquare.this.ACTIVE) {
                //------------------------------------4es hajó---------------------------------------------
                if (Gui.getSelectedShip() == 4 && !BattleSquare.this.getShip() && mNum == 0) {
                    if (Gui.newMoShip.locAvailable(mainlist, BattleSquare.this, VERTICAL)) {
                        Gui.newMoShip.setLoc(mainlist, BattleSquare.this, positionlist, VERTICAL);
                        mNum++;
                    }
                }
                //----------------------------------------------------------3as hajó------------------------------------------
                if (Gui.getSelectedShip() == 3 && !BattleSquare.this.getShip() && bNum == 0) {
                    if (Gui.newBiShip.locAvailable(mainlist, BattleSquare.this, VERTICAL)) {
                        Gui.newBiShip.setLoc(mainlist, BattleSquare.this, positionlist, VERTICAL);
                        bNum++;
                    }
                }
                //-------------------------------------------------------------2es hajó----------------------------------------------------
                if (Gui.getSelectedShip() == 2 && !BattleSquare.this.getShip() && meNum == 0) {
                    if (Gui.newMedShip.locAvailable(mainlist, BattleSquare.this, VERTICAL)) {
                        Gui.newMedShip.setLoc(mainlist, BattleSquare.this, positionlist, VERTICAL);
                        meNum++;
                    }
                }
                //-------------------------------------------------------------1es ahjó-----------------------------------------
                if (Gui.getSelectedShip() == 1 && !BattleSquare.this.getShip() && sNum == 0) {
                    if (Gui.newSmShip.locAvailable(mainlist, BattleSquare.this, VERTICAL)) {
                        Gui.newSmShip.setLoc(mainlist, BattleSquare.this, positionlist, VERTICAL);
                        sNum++;
                    }
                }
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {}

        @Override
        public void mouseReleased(MouseEvent e) {}

        @Override
        public void mouseEntered(MouseEvent e) {
            if (!BattleSquare.this.getShip()) {
                BattleSquare.this.setBackground(OnFocus_BackgroundColor);
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            if (!BattleSquare.this.getShip()) {
                BattleSquare.this.setBackground(Def_BackgroundColor);
            }
        }
    };
}